package com.xyz.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xyz.common.constant.WareConstart;
import com.xyz.gulimall.ware.entity.PurchaseDetailEntity;
import com.xyz.gulimall.ware.service.PurchaseDetailService;
import com.xyz.gulimall.ware.vo.MergeVo;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyz.common.utils.PageUtils;
import com.xyz.common.utils.Query;

import com.xyz.gulimall.ware.dao.PurchaseDao;
import com.xyz.gulimall.ware.entity.PurchaseEntity;
import com.xyz.gulimall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {


    @Resource
    PurchaseDetailService purchaseDetailService;

     @Override
     public PageUtils queryPage(Map<String, Object> params) {

         IPage<PurchaseEntity> page = this.page(
                      new Query<PurchaseEntity>().getPage(params),
                      new QueryWrapper<>()
              );
              return new PageUtils(page);
     }

    @Override
    public PageUtils unreceiveQueryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseEntity> queryWrapper = new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("status",1);
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void mergePurchase(MergeVo mergeVo) {

        Long purchaseId = mergeVo.getPurchaseId();
        if(purchaseId==null){
            //新建一个
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(WareConstart.PurchaseStatusEnum.CREATED.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();

        }

        List<Long> items = mergeVo.getItems();
        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map((i) -> {
            PurchaseDetailEntity entity = new PurchaseDetailEntity();
            entity.setId(i);
            entity.setPurchaseId(finalPurchaseId);
            entity.setStatus(WareConstart.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            return entity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);

    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void receivedUpdate(List<Long> id) {
        List<PurchaseEntity> purchaseEntities = id.stream().map(item -> {
            return this.getById(item);
        }).filter(item -> {
            if (item.getStatus() == WareConstart.PurchaseStatusEnum.ASSIGNED.getCode() || item.getStatus() == WareConstart.PurchaseStatusEnum.CREATED.getCode()) {
                return true;
            }
            return false;
        }).map(item->{
            item.setStatus(WareConstart.PurchaseStatusEnum.RECEIVE.getCode());
            item.setUpdateTime(new Date());
            return item;
        }).collect(Collectors.toList());

        this.updateBatchById(purchaseEntities);

        purchaseEntities.forEach(item->{
            List<PurchaseDetailEntity> purchaseDetailEntity = purchaseDetailService.listDetaIlByPurchaseId(item);
            List<PurchaseDetailEntity> collect = purchaseDetailEntity.stream().map(i -> {
                i.setStatus(WareConstart.PurchaseDetailStatusEnum.RECEIVE.getCode());
                return i;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(collect);
        });
    }

}